//
//  ShopConsultVC.m
//  sensen_ios
//
//  Created by pro on 16/2/25.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ShopConsultVC.h"

#import "CommitConsultVC.h" // 提交咨询界面

// 单元格
#import "ShopConsultCell.h" // 咨询单元格

// 数据
#import "ConsultCellFrameLayout.h" // 布局类

@interface ShopConsultVC () <UITableViewDataSource, UITableViewDelegate>
{
    NSInteger _page;
}
@property (nonatomic, strong) NSMutableArray *itemsAry; // 咨询数组

@property (nonatomic, strong) UITableView *tableView; // 表视图
@property (nonatomic, strong) UILabel *emptyView; // 咨询为空的时候的视图
@end

@implementation ShopConsultVC

#pragma mark - 控制器视图方法
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"购买咨询";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createLeftBarButtonItemWithImagename:nil]; // 创建导航栏返回按钮
    [self createRightBarButtonItemWithTitle:@"我要咨询"]; // 创建右侧咨询按钮
    
    [self _createViews]; // 创建视图
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:SS_NAVI_ORANGE_COLOR] forBarMetrics:UIBarMetricsDefault];
    
    // 隐藏导航栏
    MainTabBarController *main = (MainTabBarController *)self.tabBarController;
    [main hideTabBar];
    
    [self _initData];
}

#pragma mark - 获取数据
- (void)_initData {

    _page = 1;
    NSDictionary *params = @{@"pid":_pid,
                             @"page_no":@(_page),
                             @"page_size":@10,};
    [SVProgressHUD showWithStatus:@"正在加载..." maskType:SVProgressHUDMaskTypeClear];
    [BoyeHttpClient post:@"BY_Product_queryFaq" apiVerson:100 params:params success:^(AFHTTPRequestOperation *operation, id result) {

        if (_itemsAry) {
            [_itemsAry removeAllObjects];
        } else {
            _itemsAry = [[NSMutableArray alloc] init];
        }
        [_tableView.footer setHidden:NO];
        
        NSLog(@"咨询列表：%@", result);
        
        [SVProgressHUD dismiss];
        if (![result[@"list"] isKindOfClass:[NSNull class]]) {
            NSArray *list = (NSArray *)result[@"list"];
            for (NSDictionary *dic in list) {
                ConsultModel *model = [[ConsultModel alloc] initContentWithDic:dic];
                ConsultCellFrameLayout *layout = [[ConsultCellFrameLayout alloc] init];
                layout.model = model;
                [_itemsAry addObject:layout];
            }
            // 没有数据的时候就隐藏表视图
            if (_itemsAry.count == 0 || _itemsAry == nil) {
                _tableView.hidden = YES;
            } else {
            
                _emptyView.hidden = YES;
                
                // 刷新表视图
                [_tableView reloadData];
                [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
            }

        } else {
            _tableView.hidden = YES;
        }
        
        if (_itemsAry.count == [result[@"count"] integerValue]) {
            [_tableView.footer setHidden:YES];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        _tableView.hidden = YES;
        [SVProgressHUD showErrorWithStatus:error.domain withDuration:2];
    }];
    
    [_tableView.header endRefreshing];
}
- (void)_getMoreData {
    
    _page ++;
    NSDictionary *params = @{@"pid":_pid,
                             @"page_no":@(_page),
                             @"page_size":@10,};
    [SVProgressHUD showWithStatus:@"正在加载..." maskType:SVProgressHUDMaskTypeClear];
    [BoyeHttpClient post:@"BY_Product_queryFaq" apiVerson:100 params:params success:^(AFHTTPRequestOperation *operation, id result) {
        NSLog(@"咨询:%@", result);
        [SVProgressHUD dismiss];
        if (![result[@"list"] isKindOfClass:[NSNull class]]) {
            NSArray *list = (NSArray *)result[@"list"];
            for (NSDictionary *dic in list) {
                ConsultModel *model = [[ConsultModel alloc] initContentWithDic:dic];
                ConsultCellFrameLayout *layout = [[ConsultCellFrameLayout alloc] init];
                layout.model = model;
                [_itemsAry addObject:layout];
            }
            
            _emptyView.hidden = YES;
            
            // 刷新表视图
            [_tableView reloadData];
        }
        
        if (_itemsAry.count == [result[@"count"] integerValue]) {
            [_tableView.footer setHidden:YES];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.domain withDuration:2];
    }];
    
    [_tableView.footer endRefreshing];


}

#pragma mark - 创建视图
- (void)_createViews {
    
    [self _createEmptyView];
    [self _createTableView];
}
// 创建咨询为空的时候的视图
- (void)_createEmptyView {
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = 100;
    CGFloat height = 20;
    _emptyView = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _emptyView.font = FONT(15);
    _emptyView.text = @"暂无相关咨询";
    _emptyView.textColor = SS_NAVI_ORANGE_COLOR;
    _emptyView.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_emptyView];
    
    _emptyView.center = self.view.center;
}
// 创建表视图
- (void)_createTableView {
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    // 注册单元格
    [_tableView registerClass:[ShopConsultCell class] forCellReuseIdentifier:@"ShopConsultCell"];
    
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(_initData)];
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(_getMoreData)];
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _itemsAry.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ShopConsultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShopConsultCell"];
    cell.layout = _itemsAry[indexPath.section];
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return 80;
    ConsultCellFrameLayout *layout = _itemsAry[indexPath.section];
    return layout.cellHeight;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return 3;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 3;
}
// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


#pragma mark - 点击事件
- (void)rightAction:(UIButton *)button {
    
    CommitConsultVC *vc = [[CommitConsultVC alloc] init];
    vc.pid = _pid;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}




@end
